/**
 * Copyright 2009 Jesse Brown (jbrown@fluentv.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.fluentv.games.netblocks;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.Paint;

import com.fluentv.games.netblocks.piece.Shape;

/**
 * Greatly simplified version of PlayingField. Allows the specification of a Shape,
 * and displays it. Only one shape at a time is visible, and the preview area 
 * dynamically adjustes to display that shape in the best possible way.
 * 
 * @author Jesse Brown (jbrown@fluentv.com)
 */
public class PreviewView extends DrawableTetrisField {

  private static final String SHAPE_KEY = "Shape";
  
  private Shape shape;

  private final int previewCellsHigh;
  private final int previewCellsWide;

  public PreviewView(Context context, int previewCellsWide, int previewCellsHigh) {
    super(context);
    this.previewCellsWide = previewCellsWide;
    this.previewCellsHigh = previewCellsHigh;
  }

  /**
   * Set new shape, updating all dependent fields.
   * 
   * @param shape
   */
  public synchronized void setShape(Shape shape) {
    if (shape == null) {
      throw new IllegalArgumentException("null is not an acceptable shape");
    }
    this.shape = shape;
    postInvalidate();
  }
  
  /**
   * Return the current shape.
   */
  public synchronized Shape getShape() {
    return this.shape;
  }
  

  public void restoreState(SharedPreferences sharedPreferences) {
    setShape(
        Base64.<Shape>decodeFromString(sharedPreferences.getString(SHAPE_KEY, "")));
  }

  public void saveState(Editor editor) {
    editor.putString(SHAPE_KEY, 
        Base64.encodeObject(shape, Base64.DONT_BREAK_LINES | Base64.GZIP));
  }

  @Override
  protected int getBackgroundColor() {
    return Color.TRANSPARENT;
  }

  @Override
  protected Paint getBorderPaint() {
    return null;
  }

  @Override
  protected Paint getInterCellCrossHatchPaint() {
    return null;
  }

  @Override
  protected Shape getCell(int cellX, int cellY) {
    if (shape == null) {
      return null;
    }
    
    boolean masked;
    try {
      masked = shape.getMask()[cellY - 1][cellX - 1];
    } catch (ArrayIndexOutOfBoundsException e) {
      // Exceeded the area of the shape. Expected in a preview.
      return null;
    }
    
    if (masked) {
      return shape;
    } else {
      return null;
    }
  }

  @Override
  protected int getCellsHigh() {
    return previewCellsHigh;
  }

  @Override
  protected int getCellsWide() {
    return previewCellsWide;
  }  
}
